package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;


    @Autowired
    private FeeValueMapper feeValueMapper;
    
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //判断是否为新增
        boolean isUpdate = apartmentSubmitVo.getId() == null;
        //先插入公寓信息，再插入其他信息
        super.saveOrUpdate(apartmentSubmitVo);
        if(isUpdate){
            //删除图片列表
            LambdaQueryWrapper<GraphInfo> GraphInfoQueryWrapper = new LambdaQueryWrapper<>();
            GraphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            GraphInfoQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(GraphInfoQueryWrapper);
            //删除配套列表
            LambdaQueryWrapper<ApartmentFacility> ApartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(ApartmentFacilityQueryWrapper);
            //删除标签列表
            LambdaQueryWrapper<ApartmentLabel> ApartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(ApartmentLabelQueryWrapper);
            //删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> ApartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(ApartmentFeeValueQueryWrapper);
        }
        //插入图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
        if(CollectionUtils.isEmpty(graphInfoList)){
            for(GraphVo graphVo : graphVoList){
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
        }
        graphInfoService.saveBatch(graphInfoList);
        //插入配套
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        if(!CollectionUtils.isEmpty(facilityInfoIdList)){
            for(Long facilityInfoId : facilityInfoIdList){
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
//            ApartmentFacility apartmentFacility = new ApartmentFacility();
//            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
//            apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilities.add(apartmentFacility);
            }
        }
        apartmentFacilityService.saveBatch(apartmentFacilities);
        //插入标签
        List<Long> labelIdList = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        if(!CollectionUtils.isEmpty(labelIdList)){
            for(Long labelId : labelIdList){
                ApartmentLabel apartmentLabel=ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId).build();
                apartmentLabels.add(apartmentLabel);
            }
        }
        apartmentLabelService.saveBatch(apartmentLabels);
        //插入杂费
        List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        if(!CollectionUtils.isEmpty(feeValueIdList)){
            for(Long feeValueId : feeValueIdList){
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId).build();
                apartmentFeeValues.add(apartmentFeeValue);
            }
        }

        apartmentFeeValueService.saveBatch(apartmentFeeValues);
    }

    @Override
    public IPage<ApartmentItemVo> pageInfo(Page<ApartmentItemVo> apartmentItemVoPage, ApartmentQueryVo queryVo) {

        return  apartmentInfoMapper.pageInfo(apartmentItemVoPage,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo=apartmentInfoMapper.selectById(id);
        //查询图片列表
//        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
//        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
//        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> GraphVoList=graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        //查询配套列表
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartId(id);
        //查询标签列表
        List<FacilityInfo> FacilityInfoList=facilityInfoMapper.selectListByApartId(id);
        //查询杂费列表
        List<FeeValueVo> FeeValueVoList=feeValueMapper.selectListByApartId(id);
        //封装返回对象
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo );
        apartmentDetailVo.setGraphVoList(GraphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(FacilityInfoList);
        apartmentDetailVo.setFeeValueVoList(FeeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> CountQueryWrapper = new LambdaQueryWrapper<>();
        CountQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long count =roomInfoMapper.selectCount(CountQueryWrapper);
        if(count>0){
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }
        super.removeById(id);

            //删除图片列表
            LambdaQueryWrapper<GraphInfo> GraphInfoQueryWrapper = new LambdaQueryWrapper<>();
            GraphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            GraphInfoQueryWrapper.eq(GraphInfo::getItemId,id);
            graphInfoService.remove(GraphInfoQueryWrapper);
            //删除配套列表
            LambdaQueryWrapper<ApartmentFacility> ApartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(ApartmentFacilityQueryWrapper);
            //删除标签列表
            LambdaQueryWrapper<ApartmentLabel> ApartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(ApartmentLabelQueryWrapper);
            //删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> ApartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
            ApartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(ApartmentFeeValueQueryWrapper);

    }
}




